﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace Exp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string ConnString = @"server=localhost;User Id=root;password=123456;database=text;Character Set=gbk";
        private string sql;
        private MySqlConnection conn;
        private MySqlCommand comm;
       
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                using (MySqlConnection conn = new MySqlConnection(ConnString))
                {

                    conn.Open();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "操作数据库出错！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM2";
            try
            {
                serialPort1.Open();

            }
            catch
            {
                MessageBox.Show("串口未打开");
                this.Close();
            }

        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int len = serialPort1.BytesToRead;//获取可以读取的字节数
            byte[] buff = new byte[len];//创建缓存数据数组
            serialPort1.Read(buff, 0, len);//把数据读取到buff数组

            if (len == 0)
                return;

            Invoke((new Action(() =>
            {//C# 3.0以后代替委托的新方法
                if (buff[0] == 0x66 && buff[buff.Length - 1] == 0x66)//检查帧头帧尾
                {
                    //清空Chart控件
                    chart1.Series[0].Points.Clear();

                    //恢复并显示波形
                    float fs = BitConverter.ToSingle(buff, 1);
                    float k = BitConverter.ToSingle(buff, 1 + 4);
                    float bias = BitConverter.ToSingle(buff, 1 + 4 * 2);
                    int N = BitConverter.ToInt32(buff, 1 + 4 * 3);
                    float[] U = new float[N];
                    for (int i = 0; i < N; i++)
                    {
                        ushort Ui_ushort = BitConverter.ToUInt16(buff, 1 + 4 * 3 + 4 + 2 * i);
                        U[i] = Ui_ushort * k - bias;
                        chart1.Series[0].Points.AddXY(i / fs, U[i]);
                    }

                    try
                    {
                        //存储数据
                        conn = new MySqlConnection(ConnString);
                        conn.Open(); //打开数据库连接
                        comm = new MySqlCommand();
                        comm.Connection = conn;
                        comm.CommandText = "INSERT INTO wave(wavedata,wavetime) VALUES (@blobdata,NOW())";
                        comm.Parameters.Clear();
                        comm.Parameters.AddWithValue(@"blobdata", buff);
                        comm.ExecuteNonQuery();

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "操作数据库出错！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.Close();
                    }
                    finally
                    {
                        conn.Close();// 关闭数据库连接
                    }
                }

            })));
        }

    }
}
